package com.ruoyi.web.controller.system;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AdminPrizeRecord;
import com.ruoyi.system.service.IAdminPrizeRecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 开奖记录Controller
 * 
 * @author ruoyi
 * @date 2020-09-22
 */
@RestController
@RequestMapping("/system/prizeRecord")
public class AdminPrizeRecordController extends BaseController
{
    @Autowired
    private IAdminPrizeRecordService adminPrizeRecordService;

    /**
     * 查询开奖记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:prizeRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(AdminPrizeRecord adminPrizeRecord)
    {
        startPage();
        List<AdminPrizeRecord> list = adminPrizeRecordService.selectAdminPrizeRecordList(adminPrizeRecord);
        return getDataTable(list);
    }

    /**
     * 导出开奖记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:prizeRecord:export')")
    @Log(title = "开奖记录", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AdminPrizeRecord adminPrizeRecord)
    {
        List<AdminPrizeRecord> list = adminPrizeRecordService.selectAdminPrizeRecordList(adminPrizeRecord);
        ExcelUtil<AdminPrizeRecord> util = new ExcelUtil<AdminPrizeRecord>(AdminPrizeRecord.class);
        return util.exportExcel(list, "prizeRecord");
    }

    /**
     * 获取开奖记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:prizeRecord:query')")
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId)
    {
        return AjaxResult.success(adminPrizeRecordService.selectAdminPrizeRecordById(recordId));
    }

    /**
     * 新增开奖记录
     */
    @PreAuthorize("@ss.hasPermi('system:prizeRecord:add')")
    @Log(title = "开奖记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AdminPrizeRecord adminPrizeRecord)
    {
        return toAjax(adminPrizeRecordService.insertAdminPrizeRecord(adminPrizeRecord));
    }

    /**
     * 修改开奖记录
     */
    @PreAuthorize("@ss.hasPermi('system:prizeRecord:edit')")
    @Log(title = "开奖记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AdminPrizeRecord adminPrizeRecord)
    {
        String lotteryNumber = adminPrizeRecord.getLotteryNumber();
        if(StringUtils.isNotBlank(lotteryNumber)){
            try{
                String[] strings = lotteryNumber.split(",");
                if(strings.length>0){
                    HashSet<String> hs = new HashSet<>();
                    StringBuffer stringbuffer = new StringBuffer();
                    for(String s :strings){
                        Integer num = Integer.parseInt(s);
                        if(num>0 && num<=20){
                            String nums = null;
                            if (num<10) {
                                nums = "0"+num;
                            }else {
                                nums = num.toString();
                            }
                            hs.add(nums);
                            stringbuffer.append(nums).append(",");//重新拼接一次，以免用户没有在1-9数字前加0
                        }
                    }
                    if(hs.size()==8){
                        if (stringbuffer.length() > 0) {
                            String join = stringbuffer.substring(0, stringbuffer.length()-1);//去掉最后的,
                            adminPrizeRecord.setLotteryNumber(join);
                            adminPrizeRecord.setState(null);//不能更新状态，保持原有状态。否则会导致已开奖数据会更新为未开奖状态
                            return toAjax(adminPrizeRecordService.updateAdminPrizeRecord(adminPrizeRecord));
                        }
                    }
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        return AjaxResult.error("请输入8位小于或等于20不重复且以 , 隔开的开奖号码字符串");
    }

    public static void main(String[] args) {
        String[] s = new String[]{"20","19","10","12","16","04"};
        String[] ss = new String[8];
        Arrays.sort(s);
    }

    /**
     * 删除开奖记录
     */
    @PreAuthorize("@ss.hasPermi('system:prizeRecord:remove')")
    @Log(title = "开奖记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordIds}")
    public AjaxResult remove(@PathVariable Long[] recordIds)
    {
        return toAjax(adminPrizeRecordService.deleteAdminPrizeRecordByIds(recordIds));
    }
}
